package com.house365.core.util;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ZoomButtonsController;

import com.house365.core.R;
import com.house365.core.adapter.BaseExpandableListAdapter;
import com.house365.core.adapter.BaseListAdapter;
import com.house365.core.view.ListFooterView;
import com.house365.core.view.pulltorefresh.PullToRefreshExpandableListView;
import com.house365.core.view.pulltorefresh.PullToRefreshGridView;
import com.house365.core.view.pulltorefresh.PullToRefreshListView;

 
/**
 * @author py
 * @date 2012-6-7
 * @comment
 */
public class ViewUtil {

	public static void setButtonPressed(Button button, boolean pressed) {
		if (pressed) {
//			button.requestFocus();
//			button.setPressed(true);
//			button.setFocusableInTouchMode(true);
		} else {
//			button.setFocusable(false);
//			button.setPressed(false);
//			button.setFocusableInTouchMode(false);
		}
	}
	public static void setButtonPressed(ImageButton button, boolean pressed) {
		if (pressed) {
			button.requestFocus();
			button.setPressed(true);
			button.setFocusableInTouchMode(true);
		} else {
			button.setFocusable(false);
			button.setPressed(false);
			button.setFocusableInTouchMode(false);
		}
	}
	public static void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
					MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

	public static int dip2pix(DisplayMetrics dm, float dipValue) {
		final float scale = dm.density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int pix2dip(DisplayMetrics dm, float pixValue) {
		final float scale = dm.density;
		return (int) (pixValue / scale + 0.5f);
	}
	  public static void setListViewHeightBasedOnChildren(ListView listView) {
          //��ȡListView��Ӧ��Adapter
      ListAdapter listAdapter = listView.getAdapter(); 
      if (listAdapter == null) {
          // pre-condition
          return;
      }

      int totalHeight = 0;
      for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   //listAdapter.getCount()������������Ŀ
          View listItem = listAdapter.getView(i, null, listView);
          listItem.measure(0, 0);  //��������View �Ŀ��
          totalHeight += listItem.getMeasuredHeight();  //ͳ������������ܸ߶�
      }

      ViewGroup.LayoutParams params = listView.getLayoutParams();
      params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
      //listView.getDividerHeight()��ȡ�����ָ���ռ�õĸ߶�
      //params.height���õ����ListView������ʾ��Ҫ�ĸ߶�
      listView.setLayoutParams(params);
  }
	/**
	 * ͨ��PullToRefreshListView ��ݼ��سɹ�����
	 * @param context
	 * @param data
	 * @param refreshInfo
	 * @param adapter
	 * @param listView
	 * @param footerView
	 * @param errorStringId
	 */
	public static void onListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			PullToRefreshListView listView, int errorStringId) {
		onListDataComplete(context, data, refreshInfo, adapter, listView,  errorStringId,false);
	}
	 
	public static void onListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			PullToRefreshListView listView) {
		onListDataComplete(context, data, refreshInfo, adapter, listView,R.string.msg_load_error,false);
	}
	
	/**
	 * PullToRefreshListView 加载调用
	 * @param context
	 * @param data
	 * @param refreshInfo
	 * @param adapter
	 * @param listView
	 * @param footerView
	 * @param errorStringId
	 * @param showRefreshViewIfNodata,������Ϊ�յ�ʱ���Ƿ���ʾˢ����ͼ
	 */
	public static void onListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			PullToRefreshListView listView,int errorStringId,boolean showRefreshViewIfNodata) {
		if(!showRefreshViewIfNodata && (data == null || data.size()==0)){
			//listView.showRefreshView(false);
		}
		if (data == null) {
			if (refreshInfo.hasFooter) {
				listView.setFooterViewVisible(View.GONE);
				refreshInfo.hasFooter = false;
			}
			if (refreshInfo.refresh) {
				if(showRefreshViewIfNodata){
					adapter.clear();
				}
				adapter.notifyDataSetChanged();
				if(errorStringId != 0){
					Toast.makeText(context,errorStringId,
							Toast.LENGTH_SHORT).show();
				}
				//通知头部刷新成功
				listView.onRefreshComplete(TimeUtil.toDateAndTime(System.currentTimeMillis()/1000));
			} else {
				//通知底部加载陈宫
				listView.onRefreshComplete();
				if(errorStringId != 0){
					Toast.makeText(context, errorStringId,
						Toast.LENGTH_SHORT).show();
				}
			}

		} else {
			if (data.size() >= refreshInfo.getAvgpage() && !refreshInfo.hasFooter) {
				listView.setFooterViewVisible(View.VISIBLE);
				refreshInfo.hasFooter = true;
			} else if (refreshInfo.hasFooter
					&& data.size() < refreshInfo.getAvgpage()) {
				listView.setFooterViewVisible(View.GONE);
				refreshInfo.hasFooter = false;
			}
			if (refreshInfo.refresh) {
				adapter.clear();
			}
			adapter.addAll(data);
			adapter.notifyDataSetChanged();

			if (refreshInfo.refresh) {
				//通知头部刷新成功
				listView.onRefreshComplete(TimeUtil.toDateAndTime(System.currentTimeMillis()/1000));
			}else{
				//通知底部加载成功
				listView.onRefreshComplete();
			}

		}
	}
	
	
	/*****ListView加载调用****/
	public static void onListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			ListView listView, ListFooterView footerView,int errorStringId) {
		onListDataComplete(context, data, refreshInfo, adapter, listView, footerView, errorStringId,false);
	}
	public static void onListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			ListView listView, ListFooterView footerView) {
		onListDataComplete(context, data, refreshInfo, adapter, listView, footerView, R.string.msg_load_error,false);
	}
	
	public static void onListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			ListView listView, ListFooterView footerView,int errorStringId,boolean showRefreshViewIfNodata) {
		footerView.setPBGone();
		footerView.setTextView(R.string.text_more);
		if(!showRefreshViewIfNodata && (data == null || data.size()==0)){
			//listView.showRefreshView(false);
		}
		if (data == null) {
			if (refreshInfo.refresh) {
				if (refreshInfo.hasFooter) {
					//listView.removeFooterView(footerView);
					footerView.setVisibility(View.GONE);
					refreshInfo.hasFooter = false;
				}
				adapter.clear();
				adapter.notifyDataSetChanged();
				Toast.makeText(context,errorStringId,
						Toast.LENGTH_SHORT).show();
				footerView.setVisibility(View.VISIBLE);
				//listView.onRefreshComplete(TimeUtil.toDateAndTime(new Date().getTime()));
			} else {
				Toast.makeText(context, errorStringId,
						Toast.LENGTH_SHORT).show();
			}

		} else {
			if (data.size() >= refreshInfo.getAvgpage() && !refreshInfo.hasFooter) {
				refreshInfo.hasFooter = true;
				footerView.setVisibility(View.VISIBLE);
			} else if (refreshInfo.hasFooter
					&& data.size() < refreshInfo.getAvgpage()) {
				//listView.removeFooterView(footerView);
				footerView.setVisibility(View.GONE);
				refreshInfo.hasFooter = false;
			}else if(data.size() < refreshInfo.getAvgpage() && !refreshInfo.hasFooter){
				footerView.setVisibility(View.GONE);
			}else if(data.size()==0){
				footerView.setVisibility(View.GONE);
			}
			if (refreshInfo.refresh) {
				adapter.clear();
			}
			adapter.addAll(data);
			adapter.notifyDataSetChanged();

			if (refreshInfo.refresh) {
				if(refreshInfo.hasFooter){
					footerView.setVisibility(View.VISIBLE);
				}
				//listView.onRefreshComplete(TimeUtil.toDateAndTime(new Date().getTime()));
			}

		}
	}
	
	/**
	 * 加载PullToRefreshListView数据前准备工作
	 * @param refreshInfo
	 */
	public static void onPreLoadingListData(RefreshInfo refreshInfo,PullToRefreshListView listView,BaseListAdapter adapter) {
		if (refreshInfo.refresh) {
			refreshInfo.page = 1;
			listView.showRefreshView();
		}else{
			refreshInfo.page = refreshInfo.page+1;
		}
	}

	/**
	 * 加载ListView数据前准备工作
	 * @param refreshInfo
	 * @param listView
	 * @param footerView
	 */
	public static void onPreLoadingListData(RefreshInfo refreshInfo,ListView listView,ListFooterView footerView) {
		if (refreshInfo.refresh) {
			refreshInfo.page = 0;
			footerView.setPBVisible();
			footerView.setVisibility(View.VISIBLE);
			footerView.setTextView(R.string.loading);
		}else{
			refreshInfo.page = refreshInfo.page+1;
			footerView.setPBVisible();
		}
	}
	/**
	 * 加载ExpanableListView数据前准备工作
	 * @param refreshInfo
	 * @param listView
	 * @param footerView
	 */
	public static void onPreLoadingExpandableListData(RefreshInfo refreshInfo,PullToRefreshExpandableListView listView) {
		if (refreshInfo.refresh) {
			refreshInfo.page = 1;
			listView.showRefreshView();
		}else{
			refreshInfo.page = refreshInfo.page+1;
		}
	}
	/**
	 *  
	 * @param context
	 * @param data
	 * @param refreshInfo
	 * @param adapter
	 * @param listView
	 * @param footerView
	 * @param errorStringId
	 */
	public static void onExpandableListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseExpandableListAdapter adapter,
			PullToRefreshExpandableListView listView, int errorStringId) {
		onExpandableListDataComplete(context, data, refreshInfo, adapter, listView,  errorStringId,false);
	}
	public static void onExpandableListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseExpandableListAdapter adapter,
			PullToRefreshExpandableListView listView) {
		onExpandableListDataComplete(context, data, refreshInfo, adapter, listView,  R.string.msg_load_error,false);
	}
	/**
	 * PullToRefreshListView 
	 * @param context
	 * @param data
	 * @param refreshInfo
	 * @param adapter
	 * @param listView
	 * @param footerView
	 * @param errorStringId
	 * @param showRefreshViewIfNodata,
	 */
	public static void onExpandableListDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseExpandableListAdapter adapter,
			PullToRefreshExpandableListView listView, int errorStringId,boolean showRefreshViewIfNodata) {
		if(!showRefreshViewIfNodata && (data == null || data.size()==0)){
			//listView.showRefreshView(false);
		}
		if (data == null) {
			if (refreshInfo.hasFooter) {
				listView.setFooterViewVisible(View.GONE);
				refreshInfo.hasFooter = false;
			}
			if (refreshInfo.refresh) {
				if(showRefreshViewIfNodata){
					adapter.clear();
				}
				adapter.notifyDataSetChanged();
				if(errorStringId != 0){
					Toast.makeText(context,errorStringId,
							Toast.LENGTH_SHORT).show();
				}
				//通知头部刷新成功
				listView.onRefreshComplete(TimeUtil.toDateAndTime(System.currentTimeMillis()/1000));
			} else {
				//通知底部加载陈宫
				listView.onRefreshComplete();
				if(errorStringId != 0){
					Toast.makeText(context, errorStringId,
						Toast.LENGTH_SHORT).show();
				}
			}

		} else {
			if (data.size() >= refreshInfo.getAvgpage() && !refreshInfo.hasFooter) {
				listView.setFooterViewVisible(View.VISIBLE);
				refreshInfo.hasFooter = true;
			} else if (refreshInfo.hasFooter
					&& data.size() < refreshInfo.getAvgpage()) {
				listView.setFooterViewVisible(View.GONE);
				refreshInfo.hasFooter = false;
			}
			if (refreshInfo.refresh) {
				adapter.clear();
			}
			adapter.addAllGroup(data);
			adapter.notifyDataSetChanged();

			if (refreshInfo.refresh) {
				//通知头部刷新成功
				listView.onRefreshComplete(TimeUtil.toDateAndTime(System.currentTimeMillis()/1000));
			}else{
				//通知底部加载成功
				listView.onRefreshComplete();
			}

		}
	}
	
	public interface  InvalidateFooterValue{
		public boolean invalidateFooterValue(RefreshInfo refreshInfo,List data);
	}
	//���ؿ��ư�ť
	public static void setZoomControlGone(View view){
    	Class classType;
        Field field;
		try {
			classType = WebView.class;
			field = classType.getDeclaredField("mZoomButtonsController");
			field.setAccessible(true);
			ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(view);
			mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
			try {
				field.set(view, mZoomButtonsController);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} 
    }
	
	
	/**
	 * 加载PullToRefreshGridView数据前准备工作
	 * @param refreshInfo
	 */
	public static void onPreLoadingGridData(RefreshInfo refreshInfo,PullToRefreshGridView girdView,BaseListAdapter adapter) {
		if (refreshInfo.refresh) {
			refreshInfo.page = 1;
			girdView.showRefreshView();
		}else{
			refreshInfo.page = refreshInfo.page+1;
		}
	}
	
	/**
	 * notify PullToRefreshGridView data load complete
	 * @param context
	 * @param data
	 * @param refreshInfo
	 * @param adapter
	 * @param listView
	 * @param footerView
	 * @param errorStringId
	 */
	public static void onGridDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			PullToRefreshGridView listView, int errorStringId) {
		onGridDataComplete(context, data, refreshInfo, adapter, listView,  errorStringId,false);
	}
	 
	public static void onGridDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			PullToRefreshGridView listView) {
		onGridDataComplete(context, data, refreshInfo, adapter, listView,R.string.msg_load_error,false);
	}
	
	/**
	 * PullToRefreshListView 加载调用
	 * @param context
	 * @param data
	 * @param refreshInfo
	 * @param adapter
	 * @param listView
	 * @param footerView
	 * @param errorStringId
	 * @param showRefreshViewIfNodata,������Ϊ�յ�ʱ���Ƿ���ʾˢ����ͼ
	 */
	public static void onGridDataComplete(Context context, List data,
			RefreshInfo refreshInfo, BaseListAdapter adapter,
			PullToRefreshGridView listView,int errorStringId,boolean showRefreshViewIfNodata) {
		if(!showRefreshViewIfNodata && (data == null || data.size()==0)){
			//listView.showRefreshView(false);
		}
		if (data == null) {
			if (refreshInfo.hasFooter) {
				listView.setFooterViewVisible(View.GONE);
				refreshInfo.hasFooter = false;
			}
			if (refreshInfo.refresh) {
				if(showRefreshViewIfNodata){
					adapter.clear();
				}
				adapter.notifyDataSetChanged();
				if(errorStringId != 0){
					Toast.makeText(context,errorStringId,
							Toast.LENGTH_SHORT).show();
				}
				//通知头部刷新成功
				listView.onRefreshComplete(TimeUtil.toDateAndTime(System.currentTimeMillis()/1000));
			} else {
				//通知底部加载陈宫
				listView.onRefreshComplete();
				if(errorStringId != 0){
					Toast.makeText(context, errorStringId,
						Toast.LENGTH_SHORT).show();
				}
			}

		} else {
			if (data.size() >= refreshInfo.getAvgpage() && !refreshInfo.hasFooter) {
				listView.setFooterViewVisible(View.VISIBLE);
				refreshInfo.hasFooter = true;
			} else if (refreshInfo.hasFooter
					&& data.size() < refreshInfo.getAvgpage()) {
				listView.setFooterViewVisible(View.GONE);
				refreshInfo.hasFooter = false;
			}
			if (refreshInfo.refresh) {
				adapter.clear();
			}
			adapter.addAll(data);
			adapter.notifyDataSetChanged();

			if (refreshInfo.refresh) {
				//通知头部刷新成功
				listView.onRefreshComplete(TimeUtil.toDateAndTime(System.currentTimeMillis()/1000));
			}else{
				//通知底部加载成功
				listView.onRefreshComplete();
			}

		}
	}
}
